﻿$(document).ready(function () {
    iFocusChange();
});

var ImageHeight = 500; //大图高度
var FocusImgWidth = 96; //小图宽度
var AutoChangeTime = 5000; //图片自动滚动时间间隔
var PauseWhenFocus = true; //鼠标移动到图片是否暂停滚动
var PageSize = 7; //图片按钮分页显示数目

var CurrentMiddleIndex = 0; //当前处在中间的图片ID
var CurrentPageIndex = 0;
var Stop = false;

function moveElement(final_x, final_y) {
    var listLength = $("#ifocus_btn ul li").length;
    var obj = $("#ifocus_piclist");
    obj.css("top", final_y + "px");
    obj.css("left", final_x + "px");
    var btnsOld = (final_y / ImageHeight);
    var btnsNew = 0;
    if (btnsOld < 0) {
        btnsNew = -btnsOld;
    }
    var CurrentLeft = $("#focus_btn_inner").css("left"); //当前滚动按钮的位置
    CurrentLeft = parseInt(CurrentLeft);
    if (listLength <= 8 || btnsNew < 4) {
        CurrentMiddleIndex = 0;
        $("#focus_btn_inner").css("left", 0);
    } else if (btnsNew == 4) {
        CurrentMiddleIndex = btnsNew;
        $("#focus_btn_inner").css("left", 0);
    } else if (btnsNew > 4) {
        if (CurrentMiddleIndex == 0) {
            CurrentMiddleIndex = btnsNew;
            $("#focus_btn_inner").css("left", CurrentLeft - (CurrentMiddleIndex - 4) * FocusImgWidth);
        } else {
            $("#focus_btn_inner").css("left", CurrentLeft - (btnsNew - CurrentMiddleIndex) * FocusImgWidth);
            CurrentMiddleIndex = btnsNew;
        }
    }
    //clearInterval(intervalid);
    //intervalid = setInterval('autoiFocus()', AutoChangeTime);
}

function classNormal() {
    $("#ifocus_btn ul li").each(function (index) {
        $(this).attr("class", "normal");
    });
}

function classCurrent(iFocusBtnID) {
    iFocusBtnID.attr("class", "current");
}

//鼠标事件
function iFocusChange() {
    var btnAreaWidth = 0;
    $("#ifocus_btn ul li a").each(function (index) {
        if ($(this)) {
            $(this).bind("click", function () {
                //moveElement(0, -ImageHeight * index);
                $("#currentImage").attr("src", $(this).attr("href"));
                classNormal();
                classCurrent($(this).parent("li"));
                ShowMsgBlock();
                jQuery(this).blur();
                return false;
            });
            btnAreaWidth++;
        }
    });
    $("#ifocus_btn").width((FocusImgWidth + 2) * btnAreaWidth);
}
function ShowMsgBlock() {
    var index = GetCurrentIndex();
    var showMsgblock = $("#showMsgblock");
    var picdata = $("#picdatas ul");
    var title = picdata.children('li').eq(index).children("h2").html();
    var content = picdata.children('li').eq(index).children("p").html();
    showMsgblock.children("h2").html(title);
    showMsgblock.children("p").html(content);
}

//var intervalid = setInterval('autoiFocus()', AutoChangeTime);
var atuokey = false;
function autoiFocus() {
    if (!$('#ifocus')) return false;
    if (atuokey) return false;
    var listLength = $("#ifocus_btn ul li").length;
    var currentNum = GetCurrentIndex();
    classNormal();
    if (currentNum == listLength - 1) {
        moveElement(0, 0);
        classCurrent($("#ifocus_btn ul").children('li').eq(0));
    } else {
        moveElement(0, (-ImageHeight * currentNum) - ImageHeight);
        classCurrent($("#ifocus_btn ul").children('li').eq(currentNum + 1));
    }
}
function GetCurrentIndex() {
    var currentNum = 0;
    $("#ifocus_btn ul li").each(function (index) {
        if ($(this).attr("class") == "current")
            currentNum = index;
    });
    return currentNum;
}

function LeftRightClick(direction) {
    var listLength = $("#ifocus_btn ul li").length;
    var currentNum = GetCurrentIndex();
    classNormal();
    if (direction == 1) {
        if (currentNum == listLength - 1) {
            //moveElement(0, 0);
            $("#currentImage").attr("src", $("#ifocus_btn ul").children('li').eq(0).children("a").attr("href"));
            classCurrent($("#ifocus_btn ul").children('li').eq(0));
        } else {
            //moveElement(0, (-ImageHeight * currentNum) - ImageHeight);
            $("#currentImage").attr("src", $("#ifocus_btn ul").children('li').eq(currentNum + 1).children("a").attr("href"));
            classCurrent($("#ifocus_btn ul").children('li').eq(currentNum + 1));
        }
    } else {
        if (currentNum == 0) {
            //moveElement(0, (-ImageHeight * listLength) + ImageHeight);
            $("#currentImage").attr("src", $("#ifocus_btn ul").children('li').eq(listLength - 1).children("a").attr("href"));
            classCurrent($("#ifocus_btn ul").children('li').eq(listLength - 1));
        } else {
            //moveElement(0, (-ImageHeight * currentNum) + ImageHeight);
            $("#currentImage").attr("src", $("#ifocus_btn ul").children('li').eq(currentNum - 1).children("a").attr("href"));
            classCurrent($("#ifocus_btn ul").children('li').eq(currentNum - 1));
        }
    }
    AutoChangePage();
    ShowMsgBlock();
    //clearInterval(intervalid);
    //intervalid = setInterval('autoiFocus()', AutoChangeTime);
}
function HoverArrow(direction) {
    if (direction == 1) {
        document.getElementById("btnArrowRight").style.display = "block";
    } else {
        document.getElementById("btnArrowLeft").style.display = "block";
    }
    if (PauseWhenFocus) {
        //clearInterval(intervalid);
    }
}
function OutArrow(direction) {
    if (direction == 1) {
        document.getElementById("btnArrowRight").style.display = "none";
    } else {
        document.getElementById("btnArrowLeft").style.display = "none";
    }
    if (PauseWhenFocus) {
        //intervalid = setInterval('autoiFocus()', AutoChangeTime);
    }
}
function LeftRightPageClick(direction) {
    var listLength = $("#ifocus_btn ul li").length;
    var deveri = listLength % PageSize;
    var pageCount = 1;
    if (deveri == 0) {
        pageCount = listLength / PageSize;
    } else {
        pageCount = parseInt(listLength / PageSize) + 1;
    }
    var ifocus_btn = $("#ifocus_btn");
    var CurrentLeft = $("#ifocus_btn").css("left"); //当前滚动按钮的位置
    CurrentLeft = parseInt(CurrentLeft);
    ifocus_btn.stop();
    if (direction == 1) {
        if (pageCount > CurrentPageIndex + 1) {
            //ifocus_btn.css("left", CurrentLeft - PageSize * FocusImgWidth);
            CurrentPageIndex = CurrentPageIndex + 1;
        }
    } else {
        if (CurrentPageIndex > 0 && CurrentLeft <= -PageSize * FocusImgWidth) {
            //ifocus_btn.css("left", CurrentLeft + PageSize * FocusImgWidth);
            CurrentPageIndex = CurrentPageIndex - 1;
        }
    }
    ifocus_btn.animate({ "left": -(CurrentPageIndex * PageSize * FocusImgWidth) }, 600);
    //jQuery(this).blur();
    return false;
}
function AutoChangePage() {
    var ifocus_btn = $("#ifocus_btn");
    var currentNum = GetCurrentIndex();
    pageIndex = parseInt(currentNum / PageSize);
    //ifocus_btn.css("left", -(pageIndex * PageSize * FocusImgWidth));
    ifocus_btn.stop().animate({ left: -pageIndex * PageSize * FocusImgWidth }, 600);
    CurrentPageIndex = pageIndex;
}

function OpenPageLock() {
    Stop = false;
}





